﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;

using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using FunctionPoint.Services;
using FunctionPoint.ViewModels;
using FunctionPoint.Utility;
using FunctionPoint.Common;
using System.Collections.ObjectModel;
using FunctionPoint.UserControls;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Text.Editor;
using FunctionPoint.UIManagers;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Editor;


namespace FunctionPoint
{
    [Guid("C1F525F6-E81D-4CFA-BE82-9FE623BC7F18")]
    public class FunctionPointExplorerTreeControlHost : ToolWindowPane, IFunctionPointManagerServiceSubscriber
    {

        [Import(typeof(IVsEditorAdaptersFactoryService))]
        private IVsEditorAdaptersFactoryService editorFactory { get; set; }

        private FunctionPointSelectorToolWindowViewModel _viewModel;
        private FunctionPointSelectorToolWindowViewModel ViewModel
        {
            get
            {
                if (_viewModel == null)
                    _viewModel = new FunctionPointSelectorToolWindowViewModel();

                return _viewModel;
            }
            set
            {
                _viewModel = value;
            }
        }

        private IFunctionPointManagerService _managementService = null;
        public IFunctionPointManagerService ManagementService
        {
            get
            {
                if (_managementService == null)
                    _managementService = GetService(typeof(SFunctionPointManagerService)) as IFunctionPointManagerService;

                return _managementService;

            }
        }

        public FunctionPointExplorerTreeControlHost()
            : this(ServiceProvider.GlobalProvider)
        {

        }

        public FunctionPointExplorerTreeControlHost(IServiceProvider serviceProvider) :
            base(serviceProvider)
        {
            // Set the window title reading it from the resources.
            this.Caption = "Function Explorer Tree Control";
            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            this.BitmapResourceID = 301;
            this.BitmapIndex = 1;


            FunctionPointExplorerTreeControl toolWindow = new FunctionPointExplorerTreeControl();

            ManagementService.RegisterProjectChangedSubscriber(this);

            // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
            // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
            // the object returned by the Content property.
            //base.Content = new MyControl();
            base.Content = toolWindow;
        }



        public void ProjectContextChanged(object sender, TeamFoundationServerProjectContextChangedEventArgs args)
        {
            if (ManagementService != null && args != null)
            {
                // we check for null because if we close the solution, we want to empty the window contents/queries.

                if (args.TeamProject != null)
                {
                    WorkItemStore wiStore = args.TeamProject.TeamProjectCollection.GetService<WorkItemStore>();
                    Project workItemProject = null;
                    if (wiStore != null)
                    {

                        workItemProject = wiStore.Projects[args.TeamProject.Name];

                        ITfsService tfsService = new TfsService(args.TeamProject.TeamProjectCollection.Uri.ToString());
                        TfsUtility tfsUtility = new TfsUtility(tfsService);

                        ViewModel.QueryHierarchy = new ObservableCollection<QueryItemBase>(
                            tfsUtility.GetAllQueriesAndOptionallyWorkItems(workItemProject.Name, true, false));

                        FunctionPointExplorerTreeControl toolWindow = (FunctionPointExplorerTreeControl)base.Content;

                        toolWindow.DataContext = ViewModel.QueryHierarchy;
                    }

                }
                else
                {
                    ViewModel.QueryHierarchy = new ObservableCollection<QueryItemBase>();
                    
                }
            }

        }


        
        // not used.
        public void SelectedWorkItemsChanged(object sender, string uri, string projectName)
        {
            return;
        }
    }

}

